 var uploadImgIndex = 0;
    var imgArr = [];
    function init(){
    	var u = navigator.userAgent;
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
        if(isiOS){
            document.getElementById("upload").removeAttribute("capture","camera");
        }
    }
    function selectImage(imgFile){
        var allFile = imgFile.files;
        var totalLen = allFile.length;
        if(yValidate.checkNotEmpty(imgArr) && imgArr.length>0){
            totalLen = totalLen + imgArr.length;
        }
        if(totalLen>4){
            alert("只能上传4张图片");
            return;
        }
        for(var i=0;i<allFile.length;i++){
            var file = allFile[i];
            if(yValidate.checkNotEmpty(imgArr) && imgArr.length>0){
                if(imgArr.length <4){
                    imgArr.push(file);
                }
            }else{
                imgArr.push(file);
            }
            //添加一层过滤
            var rFilter = /^(image\/bmp|image\/gif|image\/jpeg|image\/png|image\/tiff)$/i;
            if(!rFilter.test(file.type)) {
                alert("文件格式必须为图片");
                return;
            }
            var reader = new FileReader();
            reader.readAsDataURL(file); //用文件加载器加载文件
            //文件加载完成
            reader.onload = function(e) {
                //计算最后一个窗口right边距，当时处于第4个的时候，right=0
                if((allFile.length + 1)%4 == 0){
                    document.getElementById("uploadBtn").style.marginRight = "0px";
                }
                //以下就是将所有上传的图片回显到页面上，如果需要用canvas进行剪裁再回显以下代码就放入到canvas中
                var li = document.createElement('li');
                li.id = "upload_"+uploadImgIndex;
                document.getElementById("uploadBtn").style.display = "";
                uploadImgIndex++;
                li.className = "upload-li";
                li.innerHTML = '<div class="item image">'+
                    '<img class="upload-image" src="'+e.target.result+'"/>'+
                    '<img class="delete-image" src="../../assets/images/image-delete.png"/>'+
                    '</div>';
                document.getElementById("uploadUL").insertBefore(li, document.getElementById("uploadBtn"));
                var uploadArr = document.getElementById("uploadUL").querySelectorAll("li");
                var len = uploadArr.length ;
                if(len > 4){
                    document.getElementById("uploadBtn").style.display = "none";
                }
            };
            reader.onloadend = function(e) {
                $(".delete-image").off('click');
                $(".delete-image").on('click',function(){
                	// alert("dasd");
                    var li = $(this).parent().parent()[0];
                    var index = $(".upload-ul .upload-li").index(li);
                    var liId = li.id;
                    $("#"+liId).remove();
                    imgArr.splice(index,1);
                    document.getElementById("uploadBtn").style.display = "";
                });
            }
        }
    }
    
    function fileUpload(){
        var param = new FormData();
        for(var i=0; i<imgArr.length;i++){
            param.append('file[]', imgArr[i], i);
        }
        // param.append("orderId",req.id);
        // param.append("userId", bxUserData.id);
        // $("body").mLoading("show");
		console.log(param)
		// return;
        $.ajax({
            url:'http://api.heruinet.com/index.php/waveguest/login/add_store_images',
            type:'POST',
            data:param,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success:function(data){
                // $("body").mLoading("hide");
                
            },
            error:function(err){
                // $("body").mLoading("hide");
                alert( "上传失败");
            }
        });
	}
	init();